package com.ellecity06.debugtools.ui.request_details

import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.ellecity06.debugtools.adapter.BaseViewHolder
import com.ellecity06.debugtools.databinding.DebugItemDetailsBinding
import com.ellecity06.debugtools.databinding.DebugItemRequestDetailsHeaderBinding
import com.ellecity06.debugtools.db.entity.RequestEntity
import com.ellecity06.debugtools.model.ItemDetailModel
import com.ellecity06.debugtools.utils.UnitUtils
import com.ellecity06.debugtools.utils.setKeyWordColor

/**
 * @author ellecity06
 * @time 2023/4/21 15:55
 * @des
 */
internal class DebugDetailAdapter(private val onClick: ((position: Int, item: ItemDetailModel<RequestEntity>) -> Unit)? = null) :
    PagingDataAdapter<ItemDetailModel<RequestEntity>, RecyclerView.ViewHolder>(DIFF_CALLBACK) {
    companion object {
        val DIFF_CALLBACK = object : DiffUtil.ItemCallback<ItemDetailModel<RequestEntity>>() {
            override fun areItemsTheSame(oldItem: ItemDetailModel<RequestEntity>, newItem: ItemDetailModel<RequestEntity>): Boolean =
                oldItem.title == newItem.title

            override fun areContentsTheSame(oldItem: ItemDetailModel<RequestEntity>, newItem: ItemDetailModel<RequestEntity>): Boolean =
                oldItem == newItem
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (getItem(position)?.title?.isEmpty() == true) 0 else 1
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == 0) {
            return BaseViewHolder(
                DebugItemRequestDetailsHeaderBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
        } else {
            return BaseViewHolder(
                DebugItemDetailsBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        if (getItemViewType(position) == 0) {
            holder as BaseViewHolder<DebugItemRequestDetailsHeaderBinding>
            val binding = holder.binding
            val model = getItem(position)?.data
            if (model != null) {
                binding.tvUrl.text = model.url
                binding.tvMethod.text = "Method:${model.method}"
                val timeConsuming = if (model.receivedResponseAtMillis != null && model.sentRequestAtMillis != null) {
                    UnitUtils.timeUnit(model.receivedResponseAtMillis!! - model.sentRequestAtMillis!!)
                } else "--"
                binding.tvTimeConsuming.text = "Time:${timeConsuming}"
                val contentLenght = UnitUtils.fileSize(if (model.contentLength != null) model.contentLength!!.toDouble() else 0.toDouble())
                binding.tvContentLength.text = "ContentLength:${contentLenght}"
                val decrptyContentLenght =
                    UnitUtils.fileSize(if (model.decryptContentLength != null) model.decryptContentLength!!.toDouble() else 0.toDouble())
                binding.tvDecryptContentLength.text =
                    if (model.decryptContentLength == null || model.decryptContentLength == 0L) "" else "DecryptContentLength:${decrptyContentLenght}"
                binding.tvCode.text = "Code:${model.code}"
                binding.tvMediaType.text = "MediaType:${model.mediaType}"
            }


        } else {
            holder as BaseViewHolder<DebugItemDetailsBinding>
            val binding = holder.binding
            val model = getItem(position)
            binding.tvDetailsTitle.text = model?.title
            binding.tvDetails.setKeyWordColor(model?.content, model?.searchContent, Color.RED, binding.tvDetailsCount)

        }
    }
}